package com.shujia.getdata;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import utils.MysqlTool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Scanner;

public class GetDataDemo {
    public static void main(String[] args) {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);

        Connection conn = MysqlTool.getConnection();

        System.setProperty("webdriver.chrome.driver", "E:\\projects\\IdeaProjects\\bigdata27-project\\redis\\src\\main\\java\\utils\\chromedriver.exe");

        //创建WebDriver实例
        WebDriver driver = new ChromeDriver();


        // 设置代理IP和端口
//        String proxy_ip = "192.168.20.19";
//        String proxy_port = "6000";

        // 创建 Chrome WebDriver 选项
//        chrome_options = Options()
//        chrome_options.add_argument('--proxy-server={}:{}'.format(proxy_ip, proxy_port))

//        ChromeOptions options = new ChromeOptions();
//        options.addArguments("--headless"); // 启用无头模式
//        options.addArguments("--proxy-server="+proxy_ip+":"+proxy_port); // 启用无头模式

//        WebDriver driver = new ChromeDriver(options);

        //全屏显示
        Dimension dimension = new Dimension(1920, 1080);
        driver.manage().window().setSize(dimension);


        //打开百度
        driver.get("https://passport.jd.com/uc/login?ltype=logout");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        WebElement element = driver.findElement(By.xpath("//input[@id='key']"));
        System.out.println("请输入想要查询的商品: ");
        String info = sc.next();
        element.sendKeys(info);

        //点击搜索
        driver.findElement(By.xpath("//div[@id='search']/div/div[2]/button")).click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
        jsExecutor.executeScript("window.scrollTo(0, document.body.scrollHeight)");

        //J_scroll_loading/span/a
//        try {
//            driver.findElement(By.xpath("//div[@id='J_scroll_loading']/span/a")).click();
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        try {
            Thread.sleep(5);
            getOnePageData(driver,conn);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }






        driver.quit();


    }

    public static void getOnePageData(WebDriver driver,Connection conn ) {
        List<WebElement> elements = driver.findElements(By.xpath("//div[@id='J_goodsList']/ul/li"));

        try {
            PreparedStatement preparedStatement = conn.prepareStatement("insert into jd_goods(name,price,shop,commit) values(?,?,?,?)");

            for (WebElement webElement : elements) {
                //每一个商品中的每个属性
                List<WebElement> elements1 = webElement.findElements(By.xpath("./div/div"));

                //定义变量
                String price = null;
                String name = null;
                String commit = null;
                String shop = null;

                for (WebElement webElement1 : elements1) {
                    String className = webElement1.getAttribute("class");
                    //p-price 价格
                    //p-name p-name-type-2 商品名称
                    //p-commit 评价数
                    //p-shop店铺
                    if ("p-price".equals(className)) {
                        price = webElement1.getText();
                    } else if ("p-name p-name-type-2".equals(className)) {
                        name = webElement1.getText();
                    } else if ("p-commit".equals(className)) {
                        commit = webElement1.getText();
                    } else if ("p-shop".equals(className)) {
                        shop = webElement1.getText();
                    }
                }
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,price);
                preparedStatement.setString(3,shop);
                preparedStatement.setString(4,commit);

                preparedStatement.executeUpdate();
                System.out.println(name + "| " + price + "| " + shop + "| " + commit);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }




    }
}
